import React, { Component } from "react";
import Pubsub from 'pubsub-js'
import axios from "axios";

export default class index extends Component {
  search = () => {
    //Pubsub.publish('at',{name:'tom',age:18})
    const {keyWordElement: { value: keyWord }} = this;
    //this.props.updateAppState({isFirst:false,isLoading:true})
    Pubsub.publish('at',{isFirst:false,isLoading:true})
    axios.get(`/api1/search/users?q=${keyWord}`).then(
      (res) => {
        // this.props.updateAppState({users:res.data.items,isLoading:false})
        Pubsub.publish('at',{users:res.data.items,isLoading:false})  

      },
      (err) => {
        //console.log("失败", err);
        // this.props.updateAppState({err:err,isLoading:false})
        Pubsub.publish('at',{err:err,isLoading:false})  
        
      }
    );
    // axios.get(`/api1/search/users?q=${keyWord}`).then(
    // 	response => {
    // 		//请求成功后通知App更新状态
    // 		this.props.updateAppState({isLoading:false,users:response.data.items})
    // 	},
    // 	error => {
    // 		//请求失败后通知App更新状态
    // 		this.props.updateAppState({isLoading:false,err:error.message})
    // 	}
    // )
  };
  //
  render() {
    return (
      <section className="jumbotron">
        <h3 className="jumbotron-heading"> 搜索github用户 </h3>{" "}
        <div>
          <input
            type="text"
            placeholder="输入关键词搜索"
            ref={(e) => (this.keyWordElement = e)}
          />{" "}
          &nbsp; <button onClick={this.search}> 搜索 </button>{" "}
        </div>{" "}
      </section>
    );
  }
}
